package com.zmn.mcc.dubbo.impl.staff;

import com.dingtalk.api.response.OapiSnsGetPersistentCodeResponse;
import com.taobao.api.ApiException;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.mcc.business.interfaces.staff.dingtalk.DingTalkService;
import com.zmn.mcc.common.dto.staff.StaffDRO;
import com.zmn.mcc.dubbo.dto.DubboConsts;
import com.zmn.mcc.dubbo.interfaces.staff.StaffDingTalkRemoteService;
import com.zmn.mcc.model.entity.staff.McStaff;
import com.zmn.mcc.model.entity.staff.McStaffQuery;
import com.zmn.mcc.services.interfaces.staff.StaffService;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * @author liubodong
 * @since 2021/03/02 17:00
 */
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class StaffDingTalkRemoteServiceImpl implements StaffDingTalkRemoteService {

    private final Logger logger = LoggerFactory.getLogger(StaffDingTalkRemoteServiceImpl.class);

    @Resource
    DingTalkService dingTalkService;
    @Resource
    StaffService staffService;

    @Override
    public ResponseDTO<String> getUserId(String code) {
        logger.info("获取员工钉钉userId,传入的code是{}", code);
        try {
            //1.请求SNSToken
            String snsToken = dingTalkService.getSnsAccessToken();
            //2.获取永久授权码
            OapiSnsGetPersistentCodeResponse response = dingTalkService.getPersistentCode(snsToken, code);
            //3.根据openid和persistent_code 获取用户授权的SNS_TOKEN
//            String userSnsToken = dingTalkService.getUserSnsToken(snsToken, response.getOpenid(), response.getPersistentCode());
//            //4.根据userSnsToken获取unionId
//            String unionid = dingTalkService.getUnionIdBySnsToken(userSnsToken);
            //5.根据开放平台token获取userId
            ResponseDTO<String> resp = dingTalkService.getUserIdByUnionId(response.getUnionid());
            logger.info("员工的钉钉userId是{}", resp.getData());
            return resp;
        } catch (ApiException e) {
            logger.error("调用钉钉接口报错", e);
            return ResponseDTO.fail("调用钉钉接口报错");
        }
    }

    @Override
    public ResponseDTO<StaffDRO> getStaffByAuthCode(@NotNull(message = "授权码不能为空") String authCode) {

        logger.debug("#mcc#getStaffByAuthCode params authCode:{}", authCode);

        ResponseDTO<String> userIdByAuthCode = dingTalkService.getUserIdByAuthCode(authCode);

        logger.debug("#mcc#getStaffByAuthCode#getUserIdByAuthCode res:{}", userIdByAuthCode);

        if (!userIdByAuthCode.isSuccess() || StringUtils.isEmpty(userIdByAuthCode.getData())) {
            return ResponseDTO.fail("未找到钉钉用户ID");
        }

        McStaffQuery query = new McStaffQuery();
        query.setDTalkId(userIdByAuthCode.getData());
        McStaff mcStaff = staffService.findForLogin(query);
        if (mcStaff == null) {
            return ResponseDTO.fail("通过钉钉用户ID未找到员工信息");
        }

        StaffDRO staffDRO = BeanMapper.map(mcStaff, StaffDRO.class);

        return ResponseDTO.success(staffDRO);
    }

}
